package com.zwj.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.entity.Student;
import com.zwj.common.service.StudentService;
import com.zwj.user.entity.UserAndRole;
import com.zwj.user.entity.UserId;
import com.zwj.user.security.PasswordSecurity;
import com.zwj.user.mapper.StudentMapper;
import com.zwj.user.service.UserAndRoleService;
import com.zwj.user.service.UserIdService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
* @author 23088
* @description 针对表【student(学生)】的数据库操作Service实现
* @createDate 2022-10-20 23:28:18
*/
@Service
@DubboService(loadbalance = RoundRobinLoadBalance.NAME)
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService {

    @Resource
    private UserIdService userIdService;

    @Resource
    private UserAndRoleService userAndRoleService;

    @Override
    public String register(Student student) {

        UserId userId = new UserId(200);
        userIdService.save(userId);
        student.setId(userId.getUserId());

        //MD5加密
        String password = student.getPassword();
        String md5Password = PasswordSecurity.Md5Encoding(password);
        student.setPassword(md5Password);

        student.setRegisterTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        boolean save = this.save(student);

        //用户与角色绑定
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setUserId(student.getId());
        userAndRole.setRoleId(5L);
        userAndRole.setCreateTime(LocalDateTime.now());

        userAndRoleService.save(userAndRole);

        if(!save)
            return "错误!";

        return "注册成功!";
    }

    @Override
    public Student getStudentById(Long id) {
        return this.getById(id);
    }
}




